package com.ly.hotel.spainterface.jg.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by ThinkPad on 2018/1/27.
 */

public class CustomCircleRing extends View {

    private Paint mPaint;
    private float mWidth = 0f;
    private float mPadding = 0f;
    private float startAngle = 0f;

    public CustomCircleRing(Context context) {
        this(context,null);
    }

    public CustomCircleRing(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CustomCircleRing(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /**
         * getMeasuredWidth()获取的是view原始的大小，也就是这个view在XML文件中配置或者是代码中设置的
         * 大小。getWidth()获取的是这个view最终显示的大小，这个大小有可能等于原始的大小也有可能不等于
         * 原始大小，getMeasureHight()和getHeight()同理。
         */
        if(getMeasuredWidth() > getHeight()){
            mWidth = getMeasuredHeight();
        }else{
            mWidth = getMeasuredWidth();
        }
        mPadding = 3;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.argb(100, 255, 255, 255));
        // 第一和第二参数表示圆心，第三个参数表示圆的半径；
        canvas.drawCircle(mWidth / 2,mWidth / 2,mWidth /2 - mPadding ,mPaint);
        mPaint.setColor(Color.WHITE);
        // 第一个参数表示最左边离左边框的距离，第二个参数最上边表示离顶部的距离，第三个参数表示最右边离左边框的距离，第四个参数表示最下边离顶部的距离；
        RectF rectF = new RectF(mPadding,mPadding,mWidth - mPadding,mWidth - mPadding);
        //第一个参数指定圆弧的外轮廓矩形区域，第二个参数圆弧起始角度，第三个参数圆弧扫过的角度，第四个参数是否将圆弧封闭
        canvas.drawArc(rectF,startAngle,80,false,mPaint);
    }

    private void initPaint(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);// 设置抗锯齿
        mPaint.setStyle(Paint.Style.STROKE);// 只绘制图形轮廓（居中描边）
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(6);// 设置圆环宽度
    }

    public void startAnim(){
        stopAnim();
        startViewAnim(0f,1f,1200);
    }

    public void stopAnim(){
        if(valueAnimator != null){
            clearAnimation();
            valueAnimator.setRepeatCount(1);
            valueAnimator.cancel();
            valueAnimator.end();
        }
    }

    ValueAnimator valueAnimator;
    //属性动画
    private ValueAnimator startViewAnim(float startF,float endF,long time){
        valueAnimator = ValueAnimator.ofFloat(startF,endF);
        valueAnimator.setDuration(time);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);// 设置成无限循环
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);// 设置循环的模式
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) valueAnimator.getAnimatedValue();
                startAngle = 360 * value;
                // 通知View更新页面
                invalidate();
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
        if(! valueAnimator.isRunning()){
            valueAnimator.start();
        }
        return valueAnimator;
    }
}